var MAIL_ACTION = {
    ONLINE: 0,
    BACK: 1,
    COMMAND: 2
};

var cp = require("child_process"),
    buildInfo;

exports.sendMail = function (details, fn, action) {
    require("colors");

    var nodemailer = require('nodemailer'),
        deployInfo = require("../../deploy-conf.json"),
        smtpTransport = nodemailer.createTransport("smtp", {
            host: 'smtp.aliyun.com',
            port: 465,
            secureConnection: true,
            requiresAuth: true,
            auth: {
                user: 'codera2015@aliyun.com',
                pass: 'Codera_2015'
            }
        }), tmp, username, now, moduleName;

    buildInfo = require("../build.json/index.json");

    if (!buildInfo.toEmails.length) {
        fn();
    }

    now = getNow();

    moduleName = deployInfo.namespaces;

    cp.exec("whoami", function (err, stdout) {
        if (!err) {
            username = stdout.replace("\n", "");

            switch (action) {
                case MAIL_ACTION.ONLINE:
                    tmp = "【" + now + "】" + username + "对" + moduleName + "模块发起【上线】操作";
                    break;
                case MAIL_ACTION.BACK:
                    tmp = "【" + now + "】" + username + "对" + moduleName + "模块发起【回滚】操作";
                    break;
                case MAIL_ACTION.COMMAND:
                    tmp = "【" + now + "】" + username + "发起【远程命令】操作";
                    break;
            }

            var mailOptions = {
                from: 'codera2015@aliyun.com',
                to: [].concat(buildInfo.toEmails),
                subject: tmp,
                text: tmp,
                html: createMailTable(details, action)
            };

            smtpTransport.sendMail(mailOptions, function (err) {
                if (err) {
                    console.log("email send service error!".red);
                    console.log(err);
                }

                fn();
            });
        }
    });
};

function createMailTable(details, action) {
    var tpl = '',
        success = details.success,
        error = details.error,
        tmp = error.concat(success);

    if (action == MAIL_ACTION.BACK) {
        tpl += "<span style='color:green'>成功" + success.length + "</span>&nbsp;&nbsp;";
        tpl += "<span style='color:red'>失败" + error.length + "</span><br/><br/>";
    } else {
        tpl += "操作服务器列表如下：<br/><br/>";
    }

    tpl += [
        "<table style='width:1024px;border:1px solid;text-align: center'>",
        "<tr>",
        "<th>URL</th>",
        "<th>信息</th>",
        "</tr>"
    ].join('');

    tmp.forEach(function (item) {
        tpl += "<tr><td>" + item.url + "</td><td>" + JSON.stringify(item.status) + "</td></tr>";
    });

    tpl += "</table>";

    return tpl;
}

function getNow() {
    var date = new Date(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds();

    return month + "/" + day + " " + hour + ":" + min + ":" + sec;
}